<?php
namespace app\web\controller\admin;

use app\admin\library\AdminController;

class Template extends AdminController{
    public function onTree(){
        
        $config = $this->app->config('template');

        $path = ROOT.DS.($config->theme??'theme');

        if (!is_dir($path)){
            throw new \Exception('模板目录不存在');
        }

        $data = $this->treeFolderFile($path,'php');

        return $data;
    }

    private function treeFolderFile($path,$ext='php'){
        $data = [];
        $list = scandir($path);
        foreach ($list as $key => $file){
            $item = [];
            if ($file == '.' || $file == '..'){
                continue;
            }
            if (is_dir($path.DS.$file)){
                $item['title'] =  $file;
                $item['children'] = $this->treeFolderFile($path.DS.$file,$ext);
            }else{
                if ($ext == pathinfo($file,FILEINFO_EXTENSION)){
                    $item['title'] =  $file;
                    $item['path'] = $path.DS.$file;
                }
            }

            $data[]= $item;
        }
        return $data;
    }
}